//
//  GQHTruckController.m
//  Xanthium
//
//  Created by GuanQinghao on 01/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model

#pragma mark Object

#pragma mark View
#import "GQHTruckView.h"

#pragma mark Controller
#import "GQHTruckController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHTruckController () {
    
#pragma mark Data
    
#pragma mark View
    /// 自定义根视图
    GQHTruckView *rootView;
}

@end


@implementation GQHTruckController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHTruckView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    [self fetchServiceData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"我的车辆";
    self.qh_rightButton.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
}

#pragma mark --Delegate

#pragma mark --TargetMethod

#pragma mark --PrivateMethod
- (void)fetchServiceData {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:USER_GET_ID forKey:@"userId"];
    
    NSString *URLString = [[NSString qh_APIURLString] stringByAppendingString:@"/buVehicleCertification/1/list"];
    
    [GQHNetHelper qh_requestGET:URLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        
        if (0 == [code intValue]) {
            DLog(@"%@---%@",message,data);\
            
            NSMutableArray *contentArray = [NSMutableArray array];
            NSMutableDictionary *responseData = [[data objectForKey:@"list"] firstObject];;
            
            /// 所有人
            NSString *carUser = [responseData objectForKey:@"carUser"];
            if (![carUser isKindOfClass:[NSNull class]]) {
                
                [contentArray qh_safetySetObject:carUser atIndex:0];
            } else {
                [contentArray qh_safetySetObject:@"" atIndex:0];
            }
            
            /// 所有人证件号
            [contentArray qh_safetySetObject:@"" atIndex:1];
            
            /// 车牌号
            NSString *licPlateNum = [responseData objectForKey:@"licPlateNum"];
            if (![licPlateNum isKindOfClass:[NSNull class]]) {
                
                [contentArray qh_safetySetObject:licPlateNum atIndex:2];
            } else {
                [contentArray qh_safetySetObject:@"" atIndex:2];
            }
            
            /// 行驶证
            NSString *qrivingLicPhoto = [responseData objectForKey:@"qrivingLicPhoto"];
            if (![qrivingLicPhoto isKindOfClass:[NSNull class]]) {
                
                [contentArray qh_safetySetObject:qrivingLicPhoto atIndex:3];
            } else {
                [contentArray qh_safetySetObject:@"" atIndex:3];
            }
            
            /// 道路运输许可证
            NSString *roadTraCerLic = [responseData objectForKey:@"roadTraCerLic"];
            if (![roadTraCerLic isKindOfClass:[NSNull class]]) {
                
                [contentArray qh_safetySetObject:roadTraCerLic atIndex:4];
            } else {
                [contentArray qh_safetySetObject:@"" atIndex:4];
            }
            
            /// 道路经营许可证号
            NSString *roadTraBuLicNum = [responseData objectForKey:@"roadTraBuLicNum"];
            if (![roadTraBuLicNum isKindOfClass:[NSNull class]]) {
                
                [contentArray qh_safetySetObject:roadTraBuLicNum atIndex:5];
            } else {
                [contentArray qh_safetySetObject:@"" atIndex:5];
            }
            
            /// 道路运输许可证号
            NSString *roadTraCerNum = [responseData objectForKey:@"roadTraCerNum"];
            if (![roadTraCerNum isKindOfClass:[NSNull class]]) {
                
                [contentArray qh_safetySetObject:roadTraCerNum atIndex:6];
            } else {
                [contentArray qh_safetySetObject:@"" atIndex:6];
            }
            
            /// 挂车牌照号
            NSString *trailerRegistrationNum = [responseData objectForKey:@"trailerRegistrationNum"];
            if (![trailerRegistrationNum isKindOfClass:[NSNull class]]) {
                
                [contentArray qh_safetySetObject:trailerRegistrationNum atIndex:7];
            } else {
                [contentArray qh_safetySetObject:@"" atIndex:7];
            }
            
            /// 车型
            NSString *vehicleclassificationcode = [responseData objectForKey:@"vehicleclassificationcode"];
            if (![vehicleclassificationcode isKindOfClass:[NSNull class]]) {
                
                [contentArray qh_safetySetObject:vehicleclassificationcode atIndex:8];
            } else {
                [contentArray qh_safetySetObject:@"" atIndex:8];
            }
            
            /// 车长
            NSString *carLength = [responseData objectForKey:@"carLength"];
            if (![carLength isKindOfClass:[NSNull class]]) {
                
                [contentArray qh_safetySetObject:carLength atIndex:9];
            } else {
                [contentArray qh_safetySetObject:@"" atIndex:9];
            }
            
            /// 载重
            NSString *carLoad = [responseData objectForKey:@"carLoad"];
            if (![carLoad isKindOfClass:[NSNull class]]) {
                
                [contentArray qh_safetySetObject:carLoad atIndex:10];
            } else {
                [contentArray qh_safetySetObject:@"" atIndex:10];
            }
            
            /// 车辆照片
            NSString *carPhoto = [responseData objectForKey:@"carPhoto"];
            if (![carPhoto isKindOfClass:[NSNull class]]) {
                
                [contentArray qh_safetySetObject:carPhoto atIndex:11];
            } else {
                [contentArray qh_safetySetObject:@"" atIndex:11];
            }
            
            rootView.qh_contentArray = contentArray;
            [rootView.qh_tableView reloadData];
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}
#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
